package cn.aj.pro.service.impl;

import cn.aj.basic.exception.BussinessException;
import cn.aj.basic.utils.BaiduAuditUtils;
import cn.aj.pet.domain.Pet;
import cn.aj.pet.domain.PetOnlineAuditLog;
import cn.aj.pro.domain.Product;
import cn.aj.pro.domain.ProductDetail;
import cn.aj.pro.domain.ProductOnlineAuditLog;
import cn.aj.pro.mapper.ProductDetailMapper;
import cn.aj.pro.mapper.ProductMapper;
import cn.aj.pro.mapper.ProductOnlineAuditLogMapper;
import cn.aj.pro.service.IProductService;
import cn.aj.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aj
 * @since 2022-12-09
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /**
    * @Title: add
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/9 0009 9:52
    * @Parameters: [product]
    * @Return void
     * 新增富文本
    */
    @Override
    @Transactional
    public void add(Product product) {
        super.add(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.add(detail);
    }

    /**
    * @Title: productOnsale
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/12/9 0009 10:08
    * @Parameters: [ids]
    * @Return void
     * 产品下架
    */
    @Override
    public void productOnsale(List<Long> ids) {
        // 判断前端的传过来的参数
        if (CollectionUtils.isEmpty(ids)) {
            throw new BussinessException("您的参数为空!");
        }
        // 先排除已经上架的,剩下就是未上架的
        List<Product> productList = productMapper.getByIdsAndStateProductOnsale(ids);
        if (CollectionUtils.isEmpty(productList)) { // 判断过滤非空
            return;
        }
        // 百度ai审核
        List<ProductOnlineAuditLog> auditLogs = new ArrayList<>();
        List<Long> productIds = new ArrayList<>();
        productList.forEach(product -> {
            StringBuilder sb = new StringBuilder();
            // 审核名称
            if (!BaiduAuditUtils.TextCensor(product.getName())) {
                sb.append("产品名称审核不通过，名称为:" + product.getName()).append(",");
            }
            // 审核图片 我们的resources是使用, 隔开的 我们需要一个一个去审核
            String resources = product.getResources();
            Arrays.stream(resources.split(",")).forEach(img -> {
                if (!BaiduAuditUtils.ImgCensor(img)) {
                    sb.append(" 产品图片审核不通过,名称为:" + product.getName());
                    //  throw new BussinessException("当前上传的图片违规,上架失败!");
                }
            });
            if (sb.length() > 0) {
                // 添加到日志表
                ProductOnlineAuditLog auditLog = new ProductOnlineAuditLog();
                auditLog.setAuditId(-1L);
                auditLog.setAuditTime(new Date());
                auditLog.setNote(sb.toString());
                auditLog.setState(0);
                auditLog.setProductId(product.getId());
                auditLogs.add(auditLog);
                return;
            }
            // 修改状态
            productIds.add(product.getId());
        });
        //	4.通过 修改状态为上架 --- 上架时间 下架时间
        if (!CollectionUtils.isEmpty(productIds)) {
            productMapper.batProchUpdateOn(productIds);
            for (Product product : productList) { // 添加上架的日志
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setAuditId(-1L);
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setProductId(product.getId());
                productOnlineAuditLog.setNote("产品为" + ":" + product.getName() + "上架成功");
                productOnlineAuditLogMapper.add(productOnlineAuditLog);
            }
        }
        if (!CollectionUtils.isEmpty(auditLogs)) {
            //	5.未通过 不修改状态添加日志
            productOnlineAuditLogMapper.batProchAdd(auditLogs);
        }
    }


    @Override
    public void productOffsale(List<Long> ids) {
        // 先查询已经上架了的,剩下就是下架的
        List<Product> productList = productMapper.getByIdsAndStateProOffsale(ids);
        productMapper.batchProUpdateOff(ids); // 下架
        for (Product product : productList) { // 添加下架的日志
            ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
            productOnlineAuditLog.setAuditId(-1L);
            productOnlineAuditLog.setAuditTime(new Date());
            productOnlineAuditLog.setState(0);
            productOnlineAuditLog.setNote("宠物为:" + ":" + product.getName() + "下架成功");
            productOnlineAuditLog.setProductId(product.getId());
            productOnlineAuditLogMapper.add(productOnlineAuditLog);
        }
    }

    @Override
    public void update(Product product) {
        super.update(product);
        productDetailMapper.update(product.getDetail());
    }
}

